package kr.hkit.androidcomplete01.ch19;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class LongTime3 extends Activity {

	int mValue;
	TextView mText;
	ProgressDialog mProgress;
	boolean mQuit;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_longtime2);
		mText = (TextView) findViewById(R.id.text);
	}

	public void mOnClick(View v) {
		if (mValue == 100)
			mValue = 0;
		mProgress = new ProgressDialog(this);
		mProgress.setTitle("Updating");
		mProgress.setMessage("Wait...");
		mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgress.setCancelable(true);//버튼이 안달려져서 멈출려면 true로
		mProgress.setOnCancelListener(new OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
				mQuit = true;
				mProgress.dismiss();
			}
		});
		mProgress.show();
		mQuit = false;
		mHandler.sendEmptyMessage(0);
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			mValue++;
			mText.setText(Integer.toString(mValue));
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				;
			}
			if (mValue < 100 && mQuit == false) {
				mProgress.setProgress(mValue);
				mHandler.sendEmptyMessage(0);
			} else {
				mProgress.dismiss();
			}
		}
	};
}
